package cn.com.cfae.iras.doc.analyze.config;


import cn.com.cfae.iras.doc.analyze.redis.RedisUtil;
import org.apache.commons.collections.SetUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisSentinelConnection;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

import java.util.*;

@Configuration
public class RedisConfig {
//    private static final String REDIS_SENTINEL_MASTER_CONFIG_PROPERTY = "spring.redis.sentinel.master";
//    private static final String REDIS_SENTINEL_NODES_CONFIG_PROPERTY = "spring.redis.sentinel.nodes";

//    @Value("${spring.redis.host}")
//    String hostName;
//    @Value("${spring.redis.port:6379}")
//    String port;
//
//    @Value("spring.redis.sentinel.nodes")
//    String nodes;
//
//    @Value("spring.redis.sentinel.master")
//    String master;

//    @Bean
//    public JedisPoolConfig createJedisPoolConfig() {
//        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//        return jedisPoolConfig;
//    }
//
//    @Bean
//    public RedisSentinelConfiguration createRedisSentinelConfiguration() {
//        String[] hostAndPorts = nodes.split(":");
//        if (ArrayUtils.isNotEmpty(hostAndPorts)) {
//            return null;
//        }
//        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration(master, new HashSet<String>(Arrays.asList(hostAndPorts)));
//        return redisSentinelConfiguration;
//    }
//
//
//    @Bean
//    public JedisConnectionFactory createJedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
//        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
//        jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
//        jedisConnectionFactory.setHostName(hostName);
//        jedisConnectionFactory.setPort(Integer.parseInt(port));
//        return jedisConnectionFactory;
//    }

    @Bean
    public RedisTemplate createRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

    @Bean
    public RedisUtil createRedisUtil(RedisTemplate redisTemplate) {
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
        return redisUtil;
    }
}
